﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO.Common;
using System.Data.Common;
using DAO.Common;
using System.Data;

namespace DAO.Headquarter
{
    public class SupplierDAO : DAO.Common.DAO
    {
        public SupplierDAO(string provider, string connStr) : base(provider, connStr) { }

        public List<Supplier> GetSuppliers()
        {
            List<Supplier> list = new List<Supplier>();
            using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
            {
                DbCommand command = DBHelper.CreateCommandWithParameters(
                    conn,
                    "getAllSuppliers",
                    CommandType.StoredProcedure,
                    null);

                conn.Open();
                using (DbDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Supplier s = new Supplier();
                        s.FromDataReader(reader);
                        list.Add(s);
                    }
                }
            }

            return list;
        }

        public void AddSupplier(Supplier supplier)
        {
            using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
            {
                DbCommand command = DBHelper.CreateCommandWithParameters(
                    conn,
                    "addSupplier",
                    CommandType.StoredProcedure,
                    supplier.ToParameterList());

                conn.Open();
                command.ExecuteNonQuery();
            }
        }
    }
}
